package com.dongdongshop.controller;

import com.dongdongshop.page.PageBean;
import com.dongdongshop.util.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TypeTempService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("temp")
public class TypeTempController {

    @DubboReference
    private TypeTempService ts;


    @RequestMapping("show")
    public String show(){
        return "/admin/type_template";
    }

    @RequestMapping("getAll")
    @ResponseBody
    public Result getAll(@RequestParam(defaultValue = "1")Integer pageNum,
                         @RequestParam(defaultValue = "5")Integer pageSize,String name){
        try {
            PageBean<TbTypeTemplate> all = ts.getAll(pageNum, pageSize, name);
            return Result.OK().setData(all);
        } catch (Exception e) {
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbTypeTemplate t){
        try {
            ts.insert(t);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.ADD_MESSAGE_ERROE);
        }
    }

    @RequestMapping("getById")
    @ResponseBody
    public Result getById(Long id){
        try {
            TbTypeTemplate byId = ts.getById(id);
            return Result.OK().setData(byId);
        } catch (Exception e) {
            return Result.response(ResultEnum.GETBYID_ERROR);
        }
    }


    @RequestMapping("update")
    @ResponseBody
    public Result update(TbTypeTemplate t){
        try {
            ts.update(t);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.UPDATE_MESSAGE_ERROE);
        }
    }

    @RequestMapping("deleteCheck")
    @ResponseBody
    public Result deleteCheck(Long[] ids){
        try {
            ts.deleteCheck(ids);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }

    }
}
